package com.jwb.util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import com.jwb.pub.model.Page;

public class MainController {

	protected String currentPageNbr;
	protected String everyPageCnt;
	
	protected Page page;
	
	public void pageInit()
	{
		if (page == null)
		{
			page = new Page();
		}
		if (currentPageNbr != null)
		{
			try
			{
				page.setCurrentPageNbr(Integer.parseInt(currentPageNbr));
			}
			catch (Exception e)
			{
				page.setCurrentPageNbr((int) 10);
				// this.setCurrentPageNbr("2");
			}
		}
		else
		{
			page.setCurrentPageNbr((int) 10);
		}
	}	
	
	public String exception(HttpServletRequest req,HttpServletResponse res,Exception e){
		req.setAttribute("error", e);
		return "errorPage";
	}
	
	
	public void exceptionResolve(HttpServletRequest req,HttpServletResponse res,Exception e){
		try {
			req.setAttribute("error", e);
			req.getRequestDispatcher("/jsp/errorPage.jsp").forward(req, res);
		} catch (ServletException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	
	
	public String exceptionSolve(HttpServletRequest req,HttpServletResponse res,Exception e){
		req.setAttribute("error", e);
		return "forward:/jsp/errorPage.jsp";
	}
	
	@ExceptionHandler
	public ModelAndView exceptionSolve2(HttpServletRequest req,HttpServletResponse res,Exception e){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("errorPage");
		mv.addObject("error", e);
		return mv;
	}
	
	
	

	public String getCurrentPageNbr() {
		return currentPageNbr;
	}

	public void setCurrentPageNbr(String currentPageNbr) {
		this.currentPageNbr = currentPageNbr;
	}

	public String getEveryPageCnt() {
		return everyPageCnt;
	}

	public void setEveryPageCnt(String everyPageCnt) {
		this.everyPageCnt = everyPageCnt;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	
	
}
